'''
005 Eat
Controls:       Rapid Touch
Description:    Press until the hungry pig is full
'''
import pygame
import spyral
import random


class game005(spyral.scene.Scene):
    def __init__(self, player):
        spyral.scene.Scene.__init__(self)
        self.root_camera = spyral.director.get_camera()
        self.camera = self.root_camera.make_child(layers = [0,1,2,3])
        self.group = spyral.sprite.Group(self.camera)
        
        self.player = player;
        self.timer = Timer.Timer(self.player.current_speed);
        
        self.pig = pig();
        self.conveyor = conveyor();
        self.food = [];
        for i in xrange(4):
            item = food(i);
            self.food.append(item);
            self.group.add(item);
        
        self.eaten = 0;
        self.max = 20;
        self.clicked = False;

        self.group.add(self.timer, self.pig, self.conveyor);
        
        
    def on_enter(self):
        bg = spyral.util.load_image(Dic.images['005bg'])
        self.camera.set_background(bg)
        
    def render(self):
        self.group.draw()
        self.root_camera.draw()
        
    def update(self, tick):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    spyral.director.pop()
                    exit(0)
                if event.key == pygame.K_r:
                    spyral.director.replace(game005(self.player))
            if (self.clicked == False):
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if (self.eaten < self.max):
                        self.food.pop(0).kill();
                        self.eaten += 1;
                    self.pig.close();
                    self.clicked = True;
            elif (self.clicked == True):
                if event.type == pygame.MOUSEBUTTONUP:
                    for item in self.food:
                        item.move()
                    if (self.eaten < self.max - 3):
                        self.conveyor.move();
                        newfood = food(3);
                        self.food.append(newfood)
                        self.group.add(newfood)
                    self.pig.open();
                    self.clicked = False;


        if (self.timer.timesUp()):
            if (self.winCondition() == False):
                self.player.decHealth();
            else:
                self.player.incCurrentGamesCompleted();
            spyral.director.pop()
        
        self.group.update();
 
    def winCondition(self):
        if (self.eaten == self.max):
            return True;
        else:
            return False;




class pig(spyral.sprite.Sprite):
    def __init__(self):
        spyral.sprite.Sprite.__init__(self);
        self.img_open = spyral.util.load_image(Dic.images['005pigopen']);
        self.img_closed = spyral.util.load_image(Dic.images['005pigclosed']);
        self.image = self.img_open;
        self.layer = 1;
        self.pos = (383, 23)
    
    def close(self):
        self.image = self.img_closed;
    
    def open(self):
        self.image = self.img_open;

class conveyor(spyral.sprite.Sprite):
    def __init__(self):
        spyral.sprite.Sprite.__init__(self);
        self.imgs = [];
        for i in xrange(9):
            num = repr(i);
            index = '005conveyor' + num;
            self.imgs.append(spyral.util.load_image(Dic.images[index]));
        self.image = self.imgs[0];
        self.layer = 2;
        self.pos = (0, 150)
        self.index = 0;
    
    def move(self):
        self.index += 1;
        if (8 < self.index):
            self.index = 0;
        self.image = self.imgs[self.index]
        
class food(spyral.sprite.Sprite):
    def __init__(self, num):
        spyral.sprite.Sprite.__init__(self);
        self.image = spyral.util.load_image(Dic.images[self.pickFood()])
        self.layer = 3;
        self.posnum = num;
        self.getPosition()

    
    def pickFood(self):
        num = random.randint(0,11)
        if (num < 10):
            num = '0' + repr(num);
        else:
            num = repr(num);
        return '005food' + num
    
    def move(self):
        self.posnum -= 1;
        self.getPosition();
    
    def getPosition(self):
        if (self.posnum == 3):
            self.pos = (5, 90)
        elif (self.posnum == 2):
            self.pos = (182, 120)
        elif (self.posnum == 1):
            self.pos = (400, 145)
        elif (self.posnum == 0):
            self.pos = (634, 219)



if __name__ == "__main__":
    import sys
    sys.path.append("C:\Users\Patrick\paybackpigs\paybackpigs")
    from frenzy import Dic
    Dic.setImgFolder('../images/')
    from frenzy import Image
    from frenzy import Timer
    from frenzy import Player
    spyral.init()
    spyral.director.init(Dic.geom['size'], False, 'Payback Pigs', 30, 30)
    spyral.director.push(game005(Player.Player()))
    spyral.director.run()

else:
    import Image
    import Dic
    import Timer;